<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>借阅管理</title>
    <link href="../../component/pear/css/pear.css" rel="stylesheet"/>
</head>
<body class="pear-container">
<!--检索组件-->
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">书名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="bookname" id="bookname" placeholder="" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">出版社</label>
                    <div class="layui-input-inline">
                        <input type="text" name="publish" id="publish" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">作者</label>
                    <div class="layui-input-inline">
                        <input type="text" name="wirter" id="wirter" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<!--信息显示组件-->
<div class="layui-card">
    <div class="layui-card-body">
        <table id="book-table" lay-filter="book-table"></table>
    </div>
</div>
<!--时间格式化组件-->
<script type="text/html" id="borrowtime">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}
</script>
<!--催还组件-->
<div style="display: none" id="showBorrow">
    <form class="layui-form" action="">
        <div class="mainBox">
            <div class="main-container">
                <table border="1" cellpadding="0">
                    <thead align="center">
                    <td >书名</td>
                    <td>借书时间</td>
                    <td>最后还书时间</td>
                    <td>作者</td>
                    <td>类别</td>
                    <td>借书人</td>
                    </thead>
                    <tbody align="center">
                    <td width="80" id="bookname1"></td>
                    <td width="180" id="borrowtime1"></td>
                    <td width="180" id="returntime1"></td>
                    <td width="80" id="wirter1"></td>
                    <td width="80" id="categary1"></td>
                    <td width="100" id="jieshuren"></td>
                    </tbody>
                </table>
            </div>
        </div>
        <div class="bottom">

            <div class="button-container">
                <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" lay-submit lay-filter="cuihuan">
                    催还
                </button>
                <button id="close" type="button" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="">
                    <i class="layui-icon layui-icon-close"></i>
                    关闭
                </button>
            </div>
        </div>
    </form>
</div>
<!--操作按钮组件-->
<script type="text/html" id="book-bar">
    <button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="power"><i
            class="layui-icon layui-icon-vercode"></i></button>
</script>
<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;

        let MODULE_PATH = "operate/";
        //设置表头信息
        let cols = [
            [
                {
                    title: '书名',
                    field: 'bookname',
                    align: 'center',
                    templet: '<div>{{d.book.bookname}}</div>'
                },
                {
                    title: 'ISBN',
                    field: 'isbn',
                    align: 'center',
                    templet: '<div>{{d.book.isbn}}</div>'

                },
                {
                    title: '出版社',
                    field: 'publish',
                    align: 'center',
                    templet: '<div>{{d.book.publish}}</div>'
                },
                {
                    title: '类别',
                    field: 'categary',
                    align: 'center',
                    templet: '<div>{{d.book.categary}}</div>'
                },
                {
                    title: '作者',
                    field: 'wirter',
                    align: 'center',
                    templet: '<div>{{d.book.wirter}}</div>'
                },
                {
                    title: '剩余数量',
                    field: 'surplus',
                    align: 'center',
                    templet: '<div>{{d.book.surplus}}</div>'
                },
                {
                    title: '总数',
                    field: 'number',
                    align: 'center',
                    sort: true,
                    templet: '<div>{{d.book.number}}</div>'
                },
                {
                    title: '借书人',
                    field: 'username',
                    align: 'center',
                    templet: '<div>{{d.artical.username}}</div>'
                },
                {
                    title: '借书时间',
                    field: 'borrowtime',
                    align: 'center',
                    templet: '#borrowtime'
                },
                {
                    title: '操作',
                    toolbar: '#book-bar',
                    align: 'center'
                }
            ]
        ]


        //查找监控提交
        form.on('submit(user-query)', function (data) {
            var created = data.borrowtime;
            if (created != "") {
                created = new Date(created);
                data.borrowtime = created;
            }
            else
                delete  data.borrowtime;
            console.log(created);
            data.field.page=1;
            data.field.limit=5;
            $ = layui.jquery
            $.ajax({
                url: "http://localhost/jieyuelist",
                data: data.field,
                dataType: "json",
                type: "GET",
                success: function (d) {

                    table.reload('book-table', {
                        where: { //设定异步数据接口的额外参数，任意设
                            bookname: data.field.bookname
                            , isbn: data.field.isbn
                            , wirter: data.field.wirter
                            ,borrowtime:data.field.borrowtime
                            ,publish:data.field.publish
                        }
                        , page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    }); //只重载数据
                }
            })

            console.log(data.field)
            // layer.alert(JSON.stringify(data.field), {
            //     title: '最终的提交信息'
            // })
            return false;
        });


        //表格数据填充
        table.render({
            elem: '#book-table',
            url: 'http://localhost/jieyuelista',
            page: true,
            cols: cols,
            skin: 'line',
            limit:2,
            limits:[2,4,6,8,10],
            toolbar: '#role-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(book-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'power') {
                window.power(obj);
            }
        });

        table.on('toolbar(book-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });







        window.power = function (obj) {
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            var form = layui.form;
            $.post('http://localhost/jieyuelist/'+data.borid,{}, function(str){
                layer.open({
                    type: 1,
                    title: '详情',
                    shade: 0.1,
                    area: ['600px', '320px'],
                    content: $('#showBorrow')
                });
                $("#bookname1").text(str.data.bookname)
                var borrowtime = layui.util.toDateString(str.data.borrowtime, 'yyyy-MM-dd HH:mm:ss');
                $("#borrowtime1").text(borrowtime)
                var returntime = layui.util.toDateString(str.data.returntime, 'yyyy-MM-dd HH:mm:ss');
                $("#returntime1").text(returntime)
                $("#jieshuren").text(str.data.username)
                $("#wirter1").text(str.data.wirter)
                $("#categary1").text(str.data.categary)


            });
            //催还
            console.log(data)
            var h=data.artical
            form.on('submit(cuihuan)', function (index) {
                var created = data.artical.created;
                if (created != "") {
                    created = new Date(created);
                    data.artical.created = created;
                }
                else
                    delete  data.field.created
                $.ajax({
                    url: "http://localhost/fasong",
                    // data: {"id":h},
                    data: data.artical,
                    dataType: "json",
                    type: "GET",
                    success: function () {
                        layer.closeAll();
                    }

                })

            });
        }

        window.refresh = function () {
            table.reload('book-table');
        }
    })
</script>
</body>
</html>
